import 'package:cached_network_image/cached_network_image.dart';
import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/core/common/model/creative_model.dart';
import 'package:flutter_demo/core/common/utils/image_utils.dart';
import 'package:flutter_demo/core/common/values/gaps.dart';
import 'package:flutter_demo/core/common/values/server.dart';
import 'package:flutter_demo/ui/pages/composite/widgets/play_list/custom_tap.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

/**
 * 纵向图片滚动
 */
class FoundVerScrollPlayList extends StatefulWidget {
  List<Resources> resources;

  FoundVerScrollPlayList({Key? key, required this.resources}) : super(key: key);

  @override
  FoundVerScrollPlayListState createState() => FoundVerScrollPlayListState();
}

class FoundVerScrollPlayListState extends State<FoundVerScrollPlayList> {
  final res = Rx<Resources?>(null);

  late SwiperController controller;

  @override
  void initState() {
    controller = SwiperController();
    super.initState();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  void deactivate() {
    controller.stopAutoplay();
    super.deactivate();
  }

  @override
  void activate() {
    controller.startAutoplay();
    super.activate();
  }

  @override
  Widget build(BuildContext context) {
    res.value = widget.resources.first;
    return Bounce(
        child: SizedBox(
          width: 109.w,
          child: Column(
            children: [
              Stack(
                children: [
                  Container(
                    width: 103.w,
                    height: 105.h,
                    decoration: BoxDecoration(
                        color: Get.theme.scaffoldBackgroundColor,
                        border: Border.all(
                            color: Color.fromARGB(255, 217, 217, 217),
                            width: 1.w),
                        borderRadius: BorderRadius.all(Radius.circular(10.r))),
                    child: Gaps.empty,
                  ),
                  Positioned.fill(
                      child: Swiper(
                    index: 0,
                    itemCount: widget.resources.length,
                    itemBuilder: (context, index) {
                      final resource = widget.resources.elementAt(index);
                      return ClipRRect(
                        borderRadius: BorderRadius.all(Radius.circular(10.r)),
                        child: CachedNetworkImage(
                            width: 105.w,
                            height: 105.w,
                            placeholder: (context, url) {
                              return Container(
                                color: Color.fromARGB(255, 245, 245, 245),
                              );
                            },
                            imageUrl:
                                resource.uiElement?.image?.imageUrl ?? ""),
                      );
                    },
                    onIndexChanged: (index) {
                      res.value = widget.resources.elementAt(index);
                    },
                    controller: controller,
                    scrollDirection: Axis.vertical,
                    itemHeight: 105.h,
                    itemWidth: 105.w,
                    autoplay: true,
                    autoplayDelay: 4000,
                    duration: 600,
                    scale: 0.8,
                  )),
                  Positioned(
                      right: 4.w,
                      child: Image.asset(
                        ImageUtils.getImagePath('icon_meta'),
                        width: 25.w,
                      )),
                ],
              ),
              Gaps.vGap5,
              //标题
              Obx(() => Text(
                    res.value?.uiElement!.mainTitle?.title ?? "",
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: const TextStyle(),
                  ))
            ],
          ),
        ),
        onPressed: () {
          //Get.toNamed('/playlist/${res.value?.resourceId}');
          logger.i("点击");
        });
  }
}
